package com.example.denglu;

import android.content.Intent;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

public class ProfileEditActivity extends AppCompatActivity {

    private static final int REQUEST_INTEREST = 100;
    private String mUsername;
    private TextView tvUsername;
    private TextView tvInterests;
    private Button btnBack;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_profile_edit);

        // 获取传递的用户名
        mUsername = getIntent().getStringExtra("username");

        // 初始化控件
        initViews();

        // 设置用户名显示
        tvUsername.setText("用户名：" + mUsername);

        // 设置按钮点击事件
        setButtonListeners();
    }

    // 初始化控件
    private void initViews() {
        tvUsername = findViewById(R.id.tv_username);
        tvInterests = findViewById(R.id.tv_interests);
        btnBack = findViewById(R.id.btn_back);
    }

    // 设置按钮点击事件
    private void setButtonListeners() {
        // 返回按钮点击事件
        btnBack.setOnClickListener(v -> finish());

        // 返回按钮触摸反馈
        btnBack.setOnTouchListener((v, event) -> {
            if (event.getAction() == MotionEvent.ACTION_DOWN) {
                v.setBackgroundColor(getResources().getColor(R.color.button_pressed));
            } else if (event.getAction() == MotionEvent.ACTION_UP
                    || event.getAction() == MotionEvent.ACTION_CANCEL) {
                v.setBackgroundColor(getResources().getColor(R.color.colorPrimary));
            }
            return false;
        });

        // 兴趣选择文本框点击事件
        tvInterests.setOnClickListener(v -> {
            Intent intent = new Intent(ProfileEditActivity.this, InterestSelectActivity.class);
            startActivityForResult(intent, REQUEST_INTEREST);
        });
    }

    // 处理从兴趣选择页返回的结果
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == REQUEST_INTEREST && resultCode == RESULT_OK && data != null) {
            // 获取选中的兴趣
            String interests = data.getStringExtra("interests");
            // 更新文本框显示
            if (interests != null && !interests.isEmpty()) {
                tvInterests.setText("兴趣：" + interests);
            }
        }
    }
}
